﻿using IBaseRepository;
using SqlSugarAndEntity.Entity;
using SqlSugarAndEntity.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Repository.Base;
using AutoRegistDependency.Attributes;
using Core.Proxy.Attributes.InterceptorAttributes;

namespace Repository.Role
{
    [Component]
    public class RoleRepository: BaseRepository<sys_roleinfo>, IRoleRepository
    {
        private IBaseMethod baseMethod;
        public RoleRepository(IBaseMethod baseMethod) : base(baseMethod)
        {
            this.baseMethod = baseMethod;
        }
        [Transaction]
        public void RoleForMenu(decimal RoleId,List<sys_rolemapmenu> map)
        {
            var db = baseMethod.Db();
            db.Deleteable<sys_rolemapmenu>().Where(t => t.RoleID == RoleId).ExecuteCommand();
            db.Insertable(map).ExecuteCommand();
        }
        [Transaction]
        public void RoleForUser(decimal RoleId, List<sys_usermaprole> map)
        {
            var db = baseMethod.Db();
            db.Deleteable<sys_usermaprole>().Where(t => t.RoleID == RoleId && map.Select(x => x.UserID).Contains(t.UserID)).ExecuteCommand();
            db.Insertable(map).ExecuteCommand();
        }
        [Transaction]
        public void DeleteRole(Expression<Func<sys_roleinfo, bool>>RoleExp, Expression<Func<sys_rolemapmenu, bool>> MapExp)
        {
            var db = baseMethod.Db();
            db.Deleteable<sys_roleinfo>().Where(RoleExp).ExecuteCommand();
            db.Deleteable<sys_rolemapmenu>().Where(MapExp).ExecuteCommand();
        }
    }
}
